﻿/*  
	------------------------------------------------------------------------
	 NQ Core Library
	
	 Homepage: http://www.awzhome.de/
	------------------------------------------------------------------------
	
	This Source Code Form is subject to the terms of the Mozilla Public
	License, v. 2.0. If a copy of the MPL was not distributed with this
	file, You can obtain one at http://mozilla.org/MPL/2.0/.
   
	The Original Code is code of NQ Core Library.

	The Initial Developer of the Original Code is Andreas Weizel.
	Portions created by the Initial Developer are
	Copyright (C) 2012 Andreas Weizel. All Rights Reserved.
	
	Contributor(s): (none)
	
	------------------------------------------------------------------------
*/

using System.Collections.Generic;

namespace AWZhome.NQ.Core
{
	/// <summary>
	/// Implements a generic helper class for building collection services.
	/// </summary>
	/// <typeparam name="K">The type of the collection key.</typeparam>
	/// <typeparam name="V">The service type that is managed by the collection service.</typeparam>
	/// <remarks>
	/// <para>
	/// This class is not necessary to create a collection service, but it helps to reduce the amount of code needed
	/// to implement the default functionality of such a service.
	/// </para>
	/// <para>
	/// To access the internal collection for service instances, access the
	/// <see cref="AWZhome.NQ.Core.NQCollectionServiceBase{K,V}.ServiceCollection">ServiceCollection</see>.
	/// </para>
	/// </remarks>
	public class NQCollectionServiceBase<K, V> : INQCollectionService<K, V>
	{
		private Dictionary<K, V> _serviceCollection = new Dictionary<K, V>();

		#region INQCollectionService<K,V> Member

		public V this[K key]
		{
			get
			{
				if (_serviceCollection.ContainsKey(key))
				{
					return _serviceCollection[key];
				}
				else
				{
					return default(V);
				}
			}
		}

		public int Count
		{
			get
			{
				return _serviceCollection.Count;
			}
		}

		#endregion

		#region INQService Member

		public virtual void InitService()
		{

		}

		public virtual void DestroyService()
		{

		}

		#endregion

		#region IEnumerable<V> Member

		public IEnumerator<V> GetEnumerator()
		{
			return ((IEnumerable<V>) _serviceCollection.Values).GetEnumerator();
		}

		#endregion

		#region IEnumerable Member

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return ((System.Collections.IEnumerable) _serviceCollection.Values).GetEnumerator();
		}

		#endregion

		#region Other public and protected members

		/// <summary>
		/// Returns the internal service collection used to manage the service instances.
		/// </summary>
		protected IDictionary<K, V> ServiceCollection
		{
			get
			{
				return _serviceCollection;
			}
		}

		#endregion
	}
}
